<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/13
 * Time: 14:44
 */

namespace app\admin\controller;



use app\common\model\FileManage;
use app\common\model\FileManageDetail;

class Projectfile extends Admin
{
    /**
     * 获取项目列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProjetctList(){
        $page = input('page',1);
        $limit = input('limit',20);
        $word = input('keyWords', '', 'trim');
        $param = [];
        if($word){
            $param['project_name|description|need_materials'] = ['like', '%'.$word.'%'];
        }
        $mdel = new FileManage();
        $count = $mdel->where($param)->count();
        if($page > ceil($count/$limit)){
            $page = ceil($count/$limit);
        }
        $list = $mdel->where($param)->order('id asc')->page($page, $limit)->select();
        return json_tab_msg($count, $page, $list);
    }

    /**
     * 新增修项目信息
     * @return \think\response\Json
     */
    public function modifyProjet(){
        $id = input('id');
        $data['project_name'] = input('project_name','','trim');
        $data['description'] = input('description','','trim');
        $data['user_name'] = input('user_name','','trim');
        $data['need_materials'] = input('need_materials','','trim');
        $data['start_date'] = input('start_date','','trim');
        $data['end_date'] = input('end_date','','trim');
        $data['common'] = input('common','','trim');
        $model = new FileManage();
        if($id){
            $res = $model->isUpdate(true)->save($data,array('id'=>$id));
            $msg = '修改';
        }else{
            $data['add_datetime'] = date('Y-m-d H:i:s');
            $res = $model->insertGetId($data);
            $msg = '添加';
        }
        if($res !== false){
            return json_msg($msg.'成功', true);
        }else{
            return json_msg($msg.'失败', true);
        }
    }

    /**
     * 获取项目详情信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetail(){
        $id = input('id');
        $fileModel = new FileManage();
        $info = $fileModel->where(['id'=>$id])->find();
        json_success_msg($info);
    }

    /**
     * 获取文件列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetailList(){
        $id = input('id');
        $page = input('page',1);
        $limit = input('limit',10);
        $detailModel = new FileManageDetail();
        $param['file_id'] = $id;
        $count = $detailModel->where($param)->count();
        if($page > ceil($count/$limit)){
            $page = ceil($count/$limit);
        }
        $list = $detailModel->where($param)->order('id asc')->page($page, $limit)->select();
        return json_tab_msg($count, $page, $list);
    }

    /**
     * 新增修改项目文件信息
     * @return \think\response\Json
     */
    public function modifyDetail(){
        $id = input('id');
        $data['file_id'] = input('file_id','','trim');
        $data['content'] = input('content','','trim');
        $data['file_url'] = input('file_url','','trim');
        $model = new FileManageDetail();
        if($id){
            $res = $model->isUpdate(true)->save($data,array('id'=>$id));
            $msg = '修改';
        }else{
            $data['add_datetime'] = date('Y-m-d H:i:s');
            $res = $model->insertGetId($data);
            $msg = '添加';
        }
        if($res !== false){
            return json_msg($msg.'成功', true);
        }else{
            return json_msg($msg.'失败', true);
        }
    }
}